package com.dsmbase.corp.user.web;




import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.dsmbase.corp.user.domain.User;
import com.dsmbase.corp.user.service.UserService;

@Component
@Controller
@SessionAttributes("currentUser")
public class LoginController {
	
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	UserService userService;
	
	@RequestMapping(value = "login")
	public ModelAndView login(LoginForm form, ModelMap model) {
		
		logger.info("user login:" + form.getUsername());
		
		User user = null;
		try {
			user = userService.verifyUserLogin(form);
			model.addAttribute("currentUser", user);
		} catch (Exception e) {
			
			return new ModelAndView("error", "user",
					"LOGIN FAILURE, " + form.getUsername());
		}
		
		return new ModelAndView("project", "user", user);
	}
}
